home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 9332 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.7 KB  |  90 lines

  1. Path: mhv.net!usenet
  2. From: pioli@mhv.net (Anthony Pioli)
  3. Newsgroups: comp.lang.c++
  4. Subject: help: Templates, operators, compiler
  5. Date: Fri, 01 Mar 1996 05:53:21 GMT
  6. Organization: MHVNet, the Mid Hudson Valley's Internet connection
  7. Message-ID: <4h5ov3$rhs@over.mhv.net>
  8. NNTP-Posting-Host: ulster-port10.mhv.net
  9. X-Newsreader: Forte Free Agent 1.0.82
  10.  
  11. Hi all,
  12.  
  13.     Having a little problem here with a template class...
  14.  
  15. Want to compare two 'objects' in some methods, but the 'objects' may
  16. either be pointers to objects *or* real objects, and I want the
  17. template parameters to tell me which type of comparison to do. The
  18. pointers point to some object out in the heap that have a *constant*
  19. value throughout the programs excution. So, to be quick, I just
  20. compare the address of the objects themselves and not use the
  21. operators that the class *may* provide. If the user can't guarantee
  22. that the objects will have a constant address, I need to use the
  23. operators that they must then provide. Problem I'm having is that the
  24. compiler (Borland Turbo C++ 3.1 for Windows) is seeing two types of
  25. comparisons, one of the pointers themselves, and one for the
  26. operators, which MAY OR MAY NOT BE THERE. Thats
  27. the error - the parameter class does not always provide the operators.
  28.  
  29.  
  30. BTW- the objects are unique - no more than one copy of each
  31.  
  32. Summary:
  33.      compare pointers directly if no operators provided and know
  34. object's addresses are constant.
  35.  
  36. OR
  37.  
  38.   if objects don't have constant addresses, use the provided
  39. operators.
  40.  
  41. The template parameters will tell which is the case....
  42.  
  43. Enough words, code time:
  44. --------------------------------------------------------
  45.  
  46. template <class T, int use_operators >
  47. class NotFooAgain{
  48.     T* data_ptr;
  49.     
  50. public:
  51.   void DoSomethingSpectacular( T* thing )
  52.   {
  53.      if ( use_operators == TRUE )
  54.             ... data_ptr == thing ... // 'shallow' (?) comparison 
  55.      else
  56.            ... *data_ptr == *thing...   
  57.     // this causes error if no == operator
  58.                   // defined for class T
  59.  
  60.    } 
  61.  
  62. }
  63.  
  64.  
  65. // instantiate using pointer comparisons..
  66. NotFooAgain<Bar, TRUE> a;
  67.  
  68. // instantiate using user provided operator...
  69. NotFooAgain<Bar, FALSE> b;
  70.  
  71.  
  72. ----------------------------------------------------------
  73.  
  74. And no, I can't force the user to provide those operators all the
  75. time.
  76. I also don't want to use #ifdef stuff either ( it's !user-friendly ).
  77.  
  78. I hope there is a really simple obvious way to do this, in which case
  79. you can call me an idiot.
  80.  
  81. Lost somewhere between C and C++, 
  82. Anthony
  83.  
  84.  
  85. PS - if the syntax for the the method declaration is wrong, sorry.
  86. This is a small sample code snippet and I have not got much C++
  87. template experience. Code was only to give context to the operator
  88. expressions themselves.
  89.  
  90.